package org.wangbao.web;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wangbao.service.ContentParserService;

import com.googlecode.jsonplugin.annotations.JSON;
import com.opensymphony.xwork2.ActionSupport;

public class PaperContentAction extends ActionSupport {

	public PaperContentAction() {
	}

	private String versionId = "A02";

	private String journalId = "20100605";

	public ContentParserService contentParserService;

	private List titleList = new ArrayList();

	private List textList = new ArrayList();
	
	private int ii = 0;

	public String execute() throws Exception {

		handleVerContent();
		return "success";
	}

	/**
	 * 根据传入的期刊号和版面号获得对应的titleList 和 textList
	 * 
	 * @throws Exception
	 */
	public void handleVerContent() throws Exception {
		
		String path = contentParserService.handleWebPath() + "ds/"+ journalId + "/" + versionId;

		File xlist = new File(path);
		
		System.out.println("the jour and versio path is:"+ path);


		String[] arr = xlist.list();
		for (int i = 0; i < arr.length; i++) {
			if (arr[i].contains(".txt")) {

				StringBuffer sb = handleContent(arr[i]);

				//According to the filename length to classify doc
				if (arr[i].contains("C"))
					titleList.add(sb.toString()+":::"+arr[i]);
				else
					textList.add(sb.toString()+":::"+arr[i]);
			}

		}
	}

	public StringBuffer handleContent(String arr)
			throws UnsupportedEncodingException, FileNotFoundException,
			IOException {
		File file = new File(contentParserService.handleWebPath()
				+ "ds/" + journalId + "/" + versionId + "/" + arr);
		BufferedReader reader = new BufferedReader(
				new InputStreamReader(new FileInputStream(file),"GB2312"));
		StringBuffer sb = new StringBuffer();

		for (String line = null; (line = reader.readLine()) != null;) {
			sb.append(line);
		}
		reader.close();
		//System.out.println("the result is :"+ sb.toString());
		return sb;
	}
	
	

	@JSON(serialize=false)
	public ContentParserService getContentParserService() {
		return contentParserService;
	}

	public void setContentParserService(
			ContentParserService contentParserService) {
		this.contentParserService = contentParserService;
	}

	public String getVersionId() {
		return versionId;
	}

	public void setVersionId(String versionId) {
		this.versionId = versionId;
	}

	public String getJournalId() {
		return journalId;
	}

	public void setJournalId(String journalId) {
		this.journalId = journalId;
	}

	public List<String> getTitleList() {
		return titleList;
	}

	public void setTitleList(List titleList) {
		this.titleList = titleList;
	}

	public List getTextList() {
		return textList;
	}

	public void setTextList(List textList) {
		this.textList = textList;
	}
}
